package com.woniu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.CourseTableUtile;
import com.woniu.entity.Student1;
import com.woniu.entity.User1;
import com.woniu.pojo.Class;
import com.woniu.pojo.Course;
import com.woniu.pojo.Coursetable;
import com.woniu.service.CoursetableService;
import com.woniu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("coursetable")
public class CoursetableController {
    @Autowired
    CoursetableService coursetableService;
    @Autowired
    StudentService studentService;

    /*增加跳转*/
    @RequestMapping("addIndex")
    public String addIndex(){
        return "WEB-INF/coursetable/add";
    }
   /* 增加*/
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Coursetable coursetable){
        Map<String,Object> map =new HashMap<>();
        try {
          coursetableService.add(coursetable);
          map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
   /* 删除*/
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            coursetableService.deleteById(id);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
        /*    批量删除*/
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Map<String,Object> deleteByIds(Integer[] ids){
        Map<String,Object> map = new HashMap<>();
        try {
            if(ids!=null){
                coursetableService.deleteByIds(ids);
                map.put("result",true);
            }else{
                map.put("result","empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
   /* 编辑跳转*/
    @RequestMapping("edit")
    public String edit(Integer id, Model model){
        model.addAttribute("id",id);
        return "WEB-INF/coursetable/edit";
    }
   /* 查一个*/
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
           /* 科目*/
            List<Course> courseList = coursetableService.selectCourseAll();
            map.put("courseList",courseList);
           /* 班级*/
            List<Class> classList = coursetableService.selectClassAll();
            map.put("classList",classList);
        Coursetable coursetable = coursetableService.findOne(id);
        map.put("coursetable",coursetable);
        map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
                   /* 修改*/
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String,Object> updateById(Coursetable coursetable){
        Map<String,Object> map = new HashMap<>();
        try {
          coursetableService.updateById(coursetable);
          map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
   /* 查询跳转*/
   @RequestMapping("index")
   public String index(){
       return "WEB-INF/coursetable/index";
   }
  /*  查询，按条件*/
    @RequestMapping("selectByExample")
    @ResponseBody
    public Map<String,Object> selectByExample(Coursetable coursetable, @RequestParam(name = "pageNum",defaultValue = "1")
            Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "3")Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageNum,pageSize);
           List<Coursetable> coursetableList = coursetableService.selectByExample(coursetable);
            PageInfo<Coursetable> pageInfo = new PageInfo<>(coursetableList);
            map.put("coursetableList",coursetableList);
            map.put("pageInfo",pageInfo);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }

    /* main跳转*/
    @RequestMapping("main")
    public String main(){
        return "WEB-INF/main";
    }

    /**
     * 返回班级课表
     * @param session
     * @return
     */
    @RequestMapping("showCourseTable")
    @ResponseBody
    public Map showCourseTable(HttpSession session){

        Map<String,Object> map=new HashMap<>();
        List<CourseTableUtile> classCourse=new ArrayList<>();
        User1 user=(User1)session.getAttribute("user");
        //是学生的话返回自己班级课表
        if (user.getRoleStatus().equals("2")) {
            Student1 one = studentService.findOne(user.getUid());
            Coursetable coursetable=new Coursetable();
            coursetable.setClassId(one.getClassId().getClassId());
            classCourse = coursetableService.getClassCourse(one.getClassId().getClassId());
        }else if (user.getRoleStatus().equals("1")) {

        }

        for (CourseTableUtile e : classCourse) {
            System.out.println(e);
        }
        map.put("data", classCourse);
        map.put("code", "");
        map.put("count", classCourse.size());
        map.put("msg", 0);
        return map;
    }

}
